package jp.tkym.labs.client.geom;

import java.util.Map;

import com.google.gwt.dev.util.collect.HashMap;

public class CoordinateModel {
	final Coordinate3f coordinate;
	final String name;
	CoordinateModel parent;
	private Map<String, CoordinateModel> childlen;
	
	CoordinateModel(String name){
		this.name = name;
		this.parent = null;
		childlen = new HashMap<String, CoordinateModel>();
		this.coordinate = new Coordinate3f();
	}
	
	void add(CoordinateModel model){
		childlen.put(model.name, model);
		model.parent = this;
	}
	
	void remove(String name){
		CoordinateModel child = childlen.get(name);
		if(child == null) throw new IllegalArgumentException("name "+name + " is not exited");
		childlen.remove(name);
		child.parent = null;
	}
	
	CoordinateModel get(String... name){
		if(name.length == 0) return this;
		CoordinateModel child = childlen.get(name[0]);
		if(child == null)  throw new IllegalArgumentException("name "+name[0]+" is not existed ");
		String[] next = new String[name.length-1];
		for(int i=0; i<next.length; i++) next[i] = name[i+1];
		return child.get(next);
	}
	
	Vector3f onWorld(Vector3f p){
		Vector3f r = this.coordinate.translate(p);
		if(this.parent == null) return r;
		return parent.onWorld(r);
	}
	
	class ViewCoordinateModel{
		final CoordinateModel viewCoordinate;
		final CoordinateModel targetCoordinate;
		ViewCoordinateModel(CoordinateModel viewCoordinate, CoordinateModel targetCoordinate){
			this.viewCoordinate = viewCoordinate;
			this.targetCoordinate = targetCoordinate;
		}
		
//		Vector3f is(Vector3f p){
//			this.
//			
//			
//			Vector3f onWorld = targetCoordinate.onWorld(p);
//			
//		}
//		
//		Vector3f onWorld(Vector3f p){
//			Vector3f r = this.targetCoordinate.coordinate.translate(p);
//			if(this.parent == null) return r;
//			return parent.onWorld(r);
//		}
	}
}